//
//  MapImageView.m
//  markmap
//
//  Created by 甘世清 on 2024/3/30.
//

#import "MapImageView.h"
#import "MapDetailCollectionViewCell.h"
#import "MapImageViewController.h"
#import "MapImageViewController.h"
@implementation MapImageView

- (void)SQ_UIViewForSelfView{
    
    [self.collectionView registerClass:[MapDetailCollectionViewCell class] forCellWithReuseIdentifier:@"MapDetailCollectionViewCell"];
    self.collectionView.alwaysBounceVertical = NO;
    [self addSubview:self.collectionView];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
}

- (void)setModel:(PointModel *)model{
    _model = model;
   
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    if ([model.userid isNotEmpty]) {
        [dic setObject:model.userid forKey:@"pid"];
    }  
    [dic setObject:@"1" forKey:@"current"];
    [dic setObject:@"10" forKey:@"size"];
    [HttpTool post:Url11 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * moarray = [data objectForKey:@"records"];
        NSArray * array = [PointModel mj_objectArrayWithKeyValuesArray:moarray];
        [self.jsonArray removeAllObjects];
        [self.jsonArray addObjectsFromArray:array];
        [self.collectionView reloadData];
        if (self.jsonArray.count <=0) {
            self.hidden = YES;
        } else {
            self.hidden = NO;
        }
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
#pragma mark UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.jsonArray.count+1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    MapDetailCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MapDetailCollectionViewCell" forIndexPath:indexPath];
    if (indexPath.item == self.jsonArray.count) {
        cell.bgImage.hidden = YES;
    } else {
        cell.bgImage.hidden = NO;
        PointModel * model = [self.jsonArray objectAtIndex:indexPath.item];
        [cell.bgImage sd_setImageWithURL:SQURLImage(model.url)];
    }

  return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    return CGSizeMake(52, 52);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
        MapImageViewController * vc =[[MapImageViewController alloc]init];
        vc.model = self.model;
        [[Manager SQ_getCurrentViewController].navigationController pushViewController:vc animated:YES];
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection =  UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.userInteractionEnabled = YES;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

-(NSMutableArray*)jsonArray
{
    if (!_jsonArray) {
        _jsonArray = [[NSMutableArray alloc]init];
    }
    return _jsonArray;
}
@end
